package com.yj.rocketmq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 生产者测试类
 */
@Slf4j
@Component
public class ProducerTest {
    private static final String TOPIC_PRE = "test_topic_";
    private static final String TAG_PRE = "test_tag_";
    private static int index = 0;

    @Autowired
    private RocketMqHandler mq;

    /**
     * @Schedule需要生效的话，需要在启动类加上@EnableScheduling注解
     */
    @Scheduled(cron = "0/5 * * * * ?")
    private void sendMsg() {
        String topic = TOPIC_PRE + (index / 2);
        String tag = TAG_PRE + (index);
        String msg = "Hello RocketMQ -> " + topic + " -> " + tag;
        log.info("producer发送消息:{}", msg);
        SendResult sendResult = mq.sendMsg(topic, tag, msg);
        log.info("sendResult:{}", sendResult);
        if (++index >= 6) {
            index = 0;
        }
    }
}